function [fb_av pb_av]=pert_pop(aux_data,frame_times,ROIs,nbr_frames,draw_raw,draw_vel,draw_res)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%   mean pert (local and full field) response for whole population
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fig_pos=[262         126        1135         665];
z=4;
ids=[-1:-1:-6 1:7];
l_win=1500;
r_win=2500;
[velP,velM,velP_ind,velM_ind,velP_raw,velM_raw]=get_vel_ind_from_adata(aux_data);
stim_id=round(aux_data(3,:)*2);
shutterOpen=find(abs(diff(aux_data(1,:)>1))==1);

% Code:
% 1 = feedback
% 2 = playback
% 3 = baseline

if length(nbr_frames)>5
    order=[];
    for xnd=1:length(nbr_frames)
        if nbr_frames(xnd)==20000
            if xnd==length(nbr_frames)
                order(xnd)=3;
            else
                order(xnd)=1;
            end
        else
            order(xnd)=2;
        end
    end
else
    order=[1 2 1 2 3];
end
a=1:length(order);
order(max(a(order==2))+1:end)=3;

nbr_exp=length(order);
nbr_bas=sum(order==3);
bas_exp=[];
for lnd=1:length(nbr_bas)
    bas_exp(lnd)=shutterOpen((nbr_exp*2)-((lnd*2)-1));
end
bas_exp=min(bas_exp);
display('calculating average population activity')
nbr_cells=0;
for pnd=1:length(ROIs)
    nbr_cells=nbr_cells+size(ROIs{pnd},2);
end

fb_len=zeros(1,length(ids));
pb_len=zeros(1,length(ids));
results=struct;
results.snips={};
results.snips{1}=cell(1,length(ids));
results.snips{2}=cell(1,length(ids));
curr_cell=[];
for xnd=1:length(ROIs)
    %     display(['layer ' num2str(xnd)])
    cur_frame_times=frame_times(xnd:4:end);
    for cnd=1:length(ROIs{xnd})
        display(['z-layer: ' num2str(xnd) ' cell: ' num2str(cnd)])
        if xnd>1
            curr_cell=curr_cell+1;
            display(['cell ' num2str(curr_cell) '/' num2str(nbr_cells)])
        else
            curr_cell=cnd;
            display(['cell ' num2str(curr_cell) '/' num2str(nbr_cells)])
        end
        act=ftfil(ROIs{xnd}(cnd).activity,18,0,10);
        act=psmooth(act);
        raw_act=act/median(act);
        ROI_act=map_ROI_act_to_aux_data(raw_act,cur_frame_times,length(aux_data));
        cnt=1;
        cnt2=1;
        for tnd=1:length(order)
            counter=(tnd*2)-1;
            if order(tnd)==1
                display(['feedback ' num2str(cnt) ' *************************************'])
                [fb_snips,~,~,~]=fb_calc(stim_id,velM_ind,velM_raw,ROI_act,ids,1500,500,shutterOpen,counter);
                for snd=1:length(ids)
                    if ~isempty(fb_snips{snd})
                        results.snips{1}{snd}(:,fb_len(snd)+1:fb_len(snd)+size(fb_snips{snd},2))=fb_snips{snd};
                        fb_len(snd)=fb_len(snd)+size(fb_snips{snd},2);
                    end
                end
                cnt=cnt+1;
            elseif order(tnd)==2
                display(['playback ' num2str(cnt2) ' *************************************'])
                [pb_snips,~,~,~,~]=pb_calc(stim_id,velM_ind,velM_raw,velP_ind,ROI_act,ids,1500,1000,shutterOpen,counter);
                for snd=1:length(ids)
                    if ~isempty(pb_snips{snd})
                        results.snips{2}{snd}(:,pb_len(snd)+1:pb_len(snd)+size(pb_snips{snd},2))=pb_snips{snd};
                        pb_len(snd)=pb_len(snd)+size(pb_snips{snd},2);
                    end
                end
                cnt2=cnt2+1;
            end
        end
    end
end

fb_snips_av={};
pb_snips_av={};;
for cnd=1:length(ids)
    fb_av=[];
    pb_av=[];
    cnt=0;
    cnt2=0;
    for mnd=1:length(results.snips)
        if mnd==1
            if ~isempty(results.snips{mnd}{cnd})
                fb_av(:,cnt+1:cnt+size(results.snips{mnd}{cnd},2))=results.snips{mnd}{cnd};
                cnt=cnt+size(results.snips{mnd}{cnd},2);
            end
        else
            if ~isempty(results.snips{mnd}{cnd})
                pb_av(:,cnt2+1:cnt2+size(results.snips{mnd}{cnd},2))=results.snips{mnd}{cnd};
                cnt2=cnt2+size(results.snips{mnd}{cnd},2);
            end
        end
    end
    if ~isempty(fb_av)
        if size(fb_av,2)>1
            fb_snips_av{cnd}=mean(fb_av');
        else
            fb_snips_av{cnd}=fb_av';
        end
    else
        fb_snips_av{cnd}=[];
    end
    if ~isempty(pb_av)
        if size(pb_av,2)>1
            pb_snips_av{cnd}=mean(pb_av');
        else
            pb_snips_av{cnd}=pb_av';
        end
    else
        pb_snips_av{cnd}=[];
    end
end

fb_av=fb_snips_av;
pb_av=pb_snips_av;
if draw_raw==1 | draw_vel==1 | draw_res==1
    curYmax=0;
    for ynd=1:length(results.snips)
        snipy=[];
        for pnd=1:length(ids);
            if isempty(results.snips{ynd}{pnd})
                snipy(pnd)=0;
            else
                snipy(pnd)=max(max(results.snips{ynd}{pnd}));
            end
        end
        curYmax=max([curYmax snipy]);
    end
    curYmax=max([curYmax 2]);
    x_val=[1:l_win+r_win+1];
end

if draw_raw==1
    sequ=[1 2 3 8 9 10 4 5 6 11 12 13 7];
    for ind=1:length(results.snips)
        if order(ind)==1
            color=[1 0 0];
        elseif order(ind)==2
            color=[0 0 0];
        end
        figure;
        set(gcf,'position',fig_pos);
        for knd=1:length(sequ)
            subplot(2,7,sequ(knd))
            if knd==1
                if order(ind)==1
                    title(['feedback'])
                elseif order(ind)==2
                    title(['playback'])
                end
            end
            rectangle('position',[15 0.5 10 3],'facecolor',[0.95 0.95 0.95],'linestyle','none')
            if ~isempty(results.snips{ind}{knd})
                hold on
                plot(results.snips{ind}{knd},'color',color)
            end
            if ~isempty(results.stim{ind}{knd})
                hold on
                plot(results.stim{ind}{knd}/6,'color',[0 0 1])
            end
            if ~isempty(results.runM{ind}{knd})
                hold on
                plot(results.runM{ind}{knd}/4,'color',[1 0 0])
            end
            if order(ind)==2
                if ~isempty(results.runP{ind}{knd})
                    hold on
                    plot((results.runP{ind}{knd}/4)+0.6,'color',[0 1 0])
                end
            end
            drawnow
            axis tight
            %             axis off
            %             box off
            ylim([-1 curYmax])
            xlim([0 max(x_val)])
        end
    end
end
if draw_vel==1
    max_size=0;
    for fnd=1:length(results.snips)
        for vnd=1:size(results.snips{fnd},2)
            max_size=max([max_size size(results.snips{fnd}{vnd},2)]);
        end
    end
    
    for vnd=1:length(results.snips)
        figure;
        set(gcf,'position',fig_pos);
        for snd=1:length(ids)
            stop=0;
            tnd=snd;
            cnt=1;
            if ~isempty(results.snips{vnd}{snd})
                while stop==0
                    hold on
                    subplot(max_size,length(ids),tnd)
                    hold on
                    rectangle('position',[1500 0.5 1000 4],'facecolor',[0.95 0.95 0.95],'linestyle','none')
                    hold on
                    plot(results.velM{vnd}{snd}(:,cnt)*5,'color',[0 0 0])
                    hold on
                    plot(results.snips{vnd}{snd}(:,cnt),'color',[1 0 0])
                    if cnt==size(results.snips{vnd}{snd},2)
                        stop=1;
                    else
                        cnt=cnt+1;
                        tnd=tnd+length(ids);
                    end
                    axis tight
                    ylim([-1 curYmax])
                    xlim([-0.5 max(x_val)])
                    axis off
                end
            end
        end
    end
end

if draw_res==1
    sequ=[1 2 3 8 9 10 4 5 6 11 12 13 7];
    figure;%('color',[1 1 1]);
    set(gcf,'position',fig_pos);
    for ind=1:length(ids)
        subplot(2,7,sequ(ind))
        rectangle('position',[1500 0.5 1000 4],'facecolor',[0.95 0.95 0.95],'linestyle','none')
        if ~isempty(fb_snips_av{ind})
            hold on
            plot(fb_snips_av{ind},'color',[1 0 0],'LineWidth',2)
        end
        if ~isempty(pb_snips_av{ind})
            hold on
            plot(pb_snips_av{ind},'color',[0 0 0],'LineWidth',2)
        end
        axis tight
        axis off
        box off
        ylim([0.8 curYmax])
        xlim([0 r_win+l_win+1])
        drawnow
    end
end